import { ArgumentsHost, Catch, ExceptionFilter } from '@nestjs/common';
import { Response } from 'express';
import { TypeORMError } from 'typeorm';

// 处理 TypeORMError 异常
@Catch(TypeORMError)
export class TypeOrmExceptionFilter implements ExceptionFilter {
  catch(exception: TypeORMError, host: ArgumentsHost) {
    const { stack } = exception;
    console.log(stack);
    const ctx = host.switchToHttp();
    const response = ctx.getResponse<Response>();
    response.status(200).json({
      code: 2001,
      stack,
    });
  }
}
